// pages/index/decorcompany.js
var postData = require('../../../data/post-data.js')
var app = getApp();
var page = 0;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    clickId: -1,
    clickIds: -1,
    clickIdse: -1,
    services: '服务区域',
    the_type: "装修类型",
    the_style: "专长风格",
    icon_imagef: "https://img.xtuan.com/xcximgtest/images/materal/down.png",
    icon_imagec: "https://img.xtuan.com/xcximgtest/images/materal/down.png",
    icon_imaget: "https://img.xtuan.com/xcximgtest/images/materal/down.png",
    num: 4, //后端给的分数,显示相应的星星
    one_star: '',
    two_star: 1,
    three_star: 0,
    areaID: 0,
    decorationID: 0,
    styleID: 0,
    page: 1,
    pages: 0,
    latitude: 0,
    longitude: 0,
    getlatitude: 0,
    getlongitude: 0,
    pageEnd: false,
    cityid: 1,
    showdistance: true,
    postlists: '',
    star: '',
    halfs: ''
  },

  /**
   * 生命周期函数--监听页面加载 
   */
  onLoad: function(options) {
    var that = this
    var star = that.data.star
    //that.getlist(page)
    that.getlocal()
    setTimeout(function() {
      that.getconfig();
      that.getlist(page)
      //that.distance() 
    }, 200)
  },
  //配置请求
  getconfig: function() {
    var that = this
    //var cityID = that.data.cityID
    var id = that.data.cityid
    //console.log('获取的值为', id)
    wx.request({
      url: app.apiUrl("c=zhuangxiu/index&a=getconfig"),
      data: {
        city_id: id
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      method: 'POST',
      success: function(res) {
        //console.log(res.data)
        var city = res.data.data.area
        var decompanys = res.data.data.decoration
        var style = res.data.data.style
        that.setData({
          city: city,
          decompany: decompanys,
          specails: style,
          cityid: id
        })
      }
    })
  },

  //列表请求
  getlist: function(pg) {
    var that = this
    var id = that.data.cityid
    //console.log('列表请求的id是', id)
    var getlatitude = that.data.getlatitude
    var getlongitude = that.data.getlongitude
    pg = pg ? pg : 0;
    wx.request({
      url: app.apiUrl("c=zhuangxiu/index&a=getlist"),
      data: {
        city_id: id
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      method: 'POST',  
      success: function(res) {
        //console.log(res.data)
        var polist = res.data.data
        console.log(polist)
        var arr1 = []
        var arr2 = []
        var stars = []
        var halfs = []
        for (var i in polist) {
          var latitude = polist[i].latitude;
          var longitude = polist[i].longitude;
          var star = parseInt(polist[i].compose)
          var half = 5 - star
          if ((polist[i].latitude == null) || (polist[i].longitude == null)){
            polist[i].state = false
          }else{
            polist[i].state = true
          }
          if (half > 0.5) {
            half = 1
            star = 5 - half
          }
          arr1.push(latitude)
          arr2.push(longitude)
          stars.push(star)
          halfs.push(half)
          //console.log(star)
        }
        // for (var i=0;i<polist.length;i++) {
        //   polist[i].star = strs[i];
        // }
        //console.log(arr1)
        //console.log(arr2)
        that.setData({
          postlists: polist,
          getlatitude: arr1,
          getlongitude: arr2,
          star: stars,
          half: halfs
        })
        setTimeout(function () {
          that.star()
          that.distance()
        }, 200)
      }
    })
    // that.getlocal()
    // setTimeout(function () {
    //   that.star()
    //   that.distance()
    // }, 500)

  },
  //星星显示
  star: function() {
    var that = this
    var polist = that.data.postlists
    var star = that.data.star
    var half = that.data.half
    for (var i in polist) {
      polist[i].star = star[i];
      polist[i].half = half[i]
    }
    //console.log(polist)
    that.setData({
      postlists: polist,
    })
  },
  //获取手机经纬度
  getlocal: function(e) {
    var that = this
    var count = that.data.linner
    wx.getLocation({
      type: 'wgs84',
      success: function(res) {
        var latitude = res.latitude
        var longitude = res.longitude
        wx.setStorageSync('latitude', latitude)
        wx.setStorageSync('longitude', longitude)
        var speed = res.speed
        var accuracy = res.accuracy
        that.setData({
          latitude: latitude,
          longitude: longitude,
        })
      }
    })
  },
  //距离计算
  distance: function(e) {
    var that = this
    var la1 = wx.getStorageSync('latitude');
    var lo1 = wx.getStorageSync('longitude');
    var lati = that.data.latitude
    var longi = that.data.longitude
    if (!(la1==lati)||!(lo1==longi)){
      that.getlocal();
    }
    var postlists = that.data.postlists
    var la2 = that.data.getlatitude
    var lo2 = that.data.getlongitude
    var distance = []
    //console.log(la2.length)
    //遍历经纬度，计算距离
    for (var i = 0; i < la2.length; i++) {
      //la2[i] = lo2[i]
        var La1 = la1 * Math.PI / 180.0;
        var La2 = la2[i] * Math.PI / 180.0;
        //console.log(La2)
        var La3 = La1 - La2;
        var Lb3 = lo1 * Math.PI / 180.0 - lo2[i] * Math.PI / 180.0;
        var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(La3 / 2), 2) + Math.cos(La1) * Math.cos(La2) * Math.pow(Math.sin(Lb3 / 2), 2)));
        s = s * 6378.137; //地球半径
        s = parseInt(Math.round(s * 10000) / 10000);
        distance.push(s)
        that.setData({
          postlists: postlists
        })
     
      postlists[i].distance = distance[i];
    }
    // for (var i in  postlists) {
    //   if (postlists[i].distance == 12843 || postlists[i].distance == 12851) {
    //     postlists[i].state = false
    //   } else {
    //     postlists[i].state = true
    //   }
    // }
    //postlists.push(distance)

    //console.log(postlists)
    that.setData({
      postlists: postlists
    })
  },
  //
  onShow: function() {
    var that = this
    var ids = that.data.cityid
    wx.getStorage({ //获取本地缓存
      key: "cityid",
      success: function(res) {
        //console.log('缓存的ID是', res.data)
        var id = res.data
        if(!(ids==id)){
            that.onLoad()
            wx.pageScrollTo({
              scrollTop: 0
            })
        }
        //console.log('这个', id)
        that.setData({
          cityid: id
        });
      },
    })
    wx.getStorage({ //获取本地缓存
      key: 'latitude',
      success: function(res) {
        //console.log('缓存的latitude是', res.data)
        var id = res.data
        that.setData({
          latitude: id
        });
      },
    })
    wx.getStorage({ //获取本地缓存
      key: 'longitude',
      success: function(res) {
        //console.log('缓存的longitude是', res.data)
        var id = res.data
        that.setData({
          longitude: id
        });
      },
    })
    // setTimeout(function() {
    //   that.onLoad()
    //   //that.getconfig();
    //   //that.getlist();
    //   //that.getlocal();
    // }, 200)
    // setTimeout(function(){
    //   that.distance()
    // },500)


  },
  //头部导航点击事件
  chose_city: function() {
    var that = this
    var martop = 0;
    var positions = 'fixed'
    var the_color = "green"
    var other = "rgb(153,153,153)"
    var icondown = "https://img.xtuan.com/xcximgtest/images/materal/up_green.png"
    var sames_image = "https://img.xtuan.com/xcximgtest/images/materal/down.png"
    this.setData({
      showacerage: true,
      showhouse: false,
      showsearch: false,
      showprice: false,
      showMask: true,
      martop: martop,
      status: positions,
      the_color: the_color,
      the_hcolor: other,
      the_pcolor: other,
      icon_imagef: icondown,
      icon_imagec: sames_image,
      icon_imaget: sames_image,
    })
  },
  chose_type: function() {
    var that = this
    var martop = 0;
    var positions = 'fixed'
    var the_color = "green"
    var other = "rgb(153,153,153)"
    var icondown = "https://img.xtuan.com/xcximgtest/images/materal/up_green.png"
    var sames_image = "https://img.xtuan.com/xcximgtest/images/materal/down.png"
    this.setData({
      showhouse: true,
      showacerage: false,
      showsearch: false,
      showprice: false,
      showMask: true,
      martop: martop,
      status: positions,
      the_hcolor: the_color,
      the_color: other,
      the_pcolor: other,
      icon_imagef: sames_image,
      icon_imagec: icondown,
      icon_imaget: sames_image,
    })
  },
  chose_style: function() {
    var that = this
    var martop = 0;
    var positions = 'fixed'
    var the_color = "green"
    var other = "rgb(153,153,153)"
    var icondown = "https://img.xtuan.com/xcximgtest/images/materal/up_green.png"
    var sames_image = "https://img.xtuan.com/xcximgtest/images/materal/down.png"
    this.setData({
      showprice: true,
      showhouse: false,
      showacerage: false,
      showsearch: false,
      showMask: true,
      martop: martop,
      status: positions,
      the_pcolor: the_color,
      the_hcolor: other,
      the_color: other,
      icon_imagef: sames_image,
      icon_imagec: sames_image,
      icon_imaget: icondown,
    })
  },
  // 弹框内容选择
  choose_city: function(e) {
    var that = this;
    var length = that.data.clickId;
    var index = parseInt(e.currentTarget.id);
    var value = that.data.city[index];
    var other = "rgb(153,153,153)";
    var areaID = that.data.areaID
    var decorationID = that.data.decorationID
    var styleID = that.data.styleID
    var id = that.data.cityid
    console.log('获取的cityid', id)
    var page = 1
    console.log(index)
    setTimeout(function() {
      that.getlocal()
      that.distance()
    }, 500)
    var sames_image = "https://img.xtuan.com/xcximgtest/images/materal/down.png"
    if (this.data.clickId == e.currentTarget.id) {
      this.setData({
        clickId: +1,
        services: value
      })
      return;
    }
    wx.pageScrollTo({
      scrollTop: 0
    })
    wx.request({
      url: app.apiUrl('c=zhuangxiu/index&a=getlist'),
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        area: index,
        decoration: decorationID,
        style: styleID,
        page: page,
        city_id: id
      },
      success: function(res) {
        var polist = res.data.data
        var arr1 = []
        var arr2 = []
        var stars = []
        var halfs = []
        for (var i in polist) {
          var latitude = polist[i].latitude;
          var longitude = polist[i].longitude
          var star = parseInt(polist[i].compose)
          var half = 5 - star
          if ((polist[i].latitude == null) || (polist[i].longitude == null)) {
            polist[i].state = false
          } else {
            polist[i].state = true
          }
          if (half > 0.5) {
            half = 1
            star = 5 - half
          }
          arr1.push(latitude)
          arr2.push(longitude)
          stars.push(star)
          halfs.push(half)
        }
        that.setData({
          postlists: polist,
          getlatitude: arr1,
          getlongitude: arr2,
          star: stars,
          half: halfs
        })
      }
    })
    that.setData({
      clickId: e.currentTarget.id,
      showacerage: false,
      showMask: false,
      showprice: false,
      services: value,
      icon_imagef: sames_image,
      icon_imagec: sames_image,
      icon_imaget: sames_image,
      the_color: other,
      the_hcolor: other,
      the_pcolor: other,
      areaID: index,
      page: page
    })
    setTimeout(function() {
      that.star()
    }, 500)

    console.log(value)
    console.log(that);
  },

  choose_type: function(e) {
    var that = this
    var index = parseInt(e.currentTarget.id);
    var value = this.data.decompany[index];
    var sames_image = "https://img.xtuan.com/xcximgtest/images/materal/down.png"
    var other = "rgb(153,153,153)"
    var areaID = that.data.areaID
    var decorationID = that.data.decorationID
    var styleID = that.data.styleID
    var id = that.data.cityid
    var page = 1
    setTimeout(function() {
      that.getlocal()
      that.distance()
    }, 500)
    if (this.data.clickIds == e.currentTarget.id) {
      this.setData({
        clickIds: +1
      })
      return;
    }
    wx.pageScrollTo({
      scrollTop: 0
    })
    wx.request({
      url: app.apiUrl('c=zhuangxiu/index&a=getlist'),
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        area: areaID,
        decoration: index,
        style: styleID,
        page: page,
        city_id: id
      },
      success: function(res) {
        var polist = res.data.data
        var arr1 = []
        var arr2 = []
        var stars = []
        var halfs = []
        for (var i in polist) {
          var latitude = polist[i].latitude;
          var longitude = polist[i].longitude
          var star = parseInt(polist[i].compose)
          var half = 5 - star
          if (half > 0.5) {
            half = 1
            star = 5 - half
          }
          if ((polist[i].latitude == null) || (polist[i].longitude == null)) {
            polist[i].state = false
          } else {
            polist[i].state = true
          }
          arr1.push(latitude)
          arr2.push(longitude)
          stars.push(star)
          halfs.push(half)
        }
        that.setData({
          postlists: polist,
          getlatitude: arr1,
          getlongitude: arr2,
          star: stars,
          half: halfs
        })
      }
    })
    that.setData({
      clickIds: e.currentTarget.id,
      showhouse: false,
      showMask: false,
      showprice: false,
      showsearch: true,
      the_type: value,
      icon_imagef: sames_image,
      icon_imagec: sames_image,
      icon_imaget: sames_image,
      the_color: other,
      the_hcolor: other,
      the_pcolor: other,
      decorationID: index,
      page: page
    })
    var index = e.currentTarget.id;
    console.log(value);
    setTimeout(function() {
      that.star()
    }, 500)
  },
  choose_styles: function(e) {
    var that = this
    var length = this.data.clickIdse;
    var index = parseInt(e.currentTarget.id);
    var value = this.data.specails[index];
    var sames_image = "https://img.xtuan.com/xcximgtest/images/materal/down.png"
    var other = "rgb(153,153,153)"
    var areaID = that.data.areaID
    var decorationID = that.data.decorationID
    var styleID = that.data.styleID
    var id = that.data.cityid
    var page = 1
    setTimeout(function() {
      that.getlocal()
      that.distance()
    }, 500)
    if (this.data.clickIdse == e.currentTarget.id) {
      this.setData({
        clickIdse: +1
      })
      return;
    }
    wx.pageScrollTo({
      scrollTop: 0
    })
    wx.request({
      url: app.apiUrl('c=zhuangxiu/index&a=getlist'),
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        area: areaID,
        decoration: decorationID,
        style: index,
        page: page,
        city_id: id
      },
      success: function(res) {
        var polist = res.data.data
        var arr1 = []
        var arr2 = []
        var stars = []
        var halfs = []
        for (var i in polist) {
          var latitude = polist[i].latitude;
          var longitude = polist[i].longitude
          var star = parseInt(polist[i].compose)
          var half = 5 - star
          if (half > 0.5) {
            half = 1
            star = 5 - half
          }
          if ((polist[i].latitude == null) || (polist[i].longitude == null)) {
            polist[i].state = false
          } else {
            polist[i].state = true
          }
          stars.push(star)
          halfs.push(half)
          arr1.push(latitude)
          arr2.push(longitude)
        }
        that.setData({
          postlists: polist,
          getlatitude: arr1,
          getlongitude: arr2,
          star: stars,
          half: halfs
        })
      }
    })
    that.setData({
      clickIdse: e.currentTarget.id,
      showprice: false,
      showhouse: false,
      showMask: false,
      the_style: value,
      icon_imagef: sames_image,
      icon_imagec: sames_image,
      icon_imaget: sames_image,
      the_color: other,
      the_hcolor: other,
      the_pcolor: other,
      styleID: index,
      page: page
    })
    var index = e.currentTarget.id;
    console.log(that.data);
    setTimeout(function() {
      that.star()
    }, 500)
  },
  //上拉加载
  onReachBottom: function() {
    var that = this
    that.setData({
      page: that.data.page + 1 //后台数据需要分页，每次加载一页
    })
   
    wx.showLoading({ //显示toast
      title: '加载中...',
    });
    var page = that.data.page
    var areaID = that.data.areaID
    var decorationID = that.data.decorationID
    var styleID = that.data.styleID
    var length = that.data.clickId;
    var id = that.data.cityid
    //page = page + 1;
    wx.request({
      url: app.apiUrl('c=zhuangxiu/index&a=getlist'),
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        area: areaID,
        decoration: decorationID,
        style: styleID,
        page: that.data.page,
        city_id: id
      },
      method: 'GET',
      success: function(res) {
        console.log(res.data.data)
        //var polist = res.data.data
        if (res.data.status == 1) {
          var listdata = that.data.postlists
          var getlatitude = that.data.getlatitude
          var getlongitude = that.data.getlongitude
          var star = that.data.star
          var half = that.data.half
          for (var i = 0; i < res.data.data.length; i++) {
            listdata.push(res.data.data[i]);
            getlatitude.push(res.data.data[i].latitude);
            getlongitude.push(res.data.data[i].longitude);
            star.push(parseInt(res.data.data[i].compose));
            half.push(parseInt(res.data.data[i].half));
           
          }
          for(var i in listdata){
            if ((listdata[i].latitude == null) || (listdata[i].longitude == null)) {
              listdata[i].state = false
            } else {
              listdata[i].state = true
            }
          }
          //console.log(getlongitude)
          that.setData({
            postlists: listdata,
            getlatitude: getlatitude,
            getlongitude: getlongitude,
            star: star,
            half: half
          });
          setTimeout(function() {
            that.star()
              that.getlocal()
              that.distance()
          }, 200)
        } else if (res.data.status == 0) {
          wx.showLoading({
            title: '加载完毕', 
          })
        }
        //console.log(page)
      },
      fail: function() {
        // fail
      },
      complete: function() {
        // complete
        wx.hideNavigationBarLoading() //完成停止加载
        wx.hideLoading(); //隐藏toast
        // setTimeout(function() {
        //   wx.showToast({
        //     title: '没有更多数据了！',
        //   })
        // }, 2000)
      }
    })

  },
  //下拉刷新
  onPullDownRefresh: function() {
    var that = this
    var page = that.data.page
    var areaID = that.data.areaID
    var decorationID = that.data.decorationID
    var styleID = that.data.styleID
    var id = that.data.cityid
    setTimeout(function() {
      that.getlocal()
      that.distance()
    }, 500)

    // wx.showLoading({
    //   title: '数据加载中',
    // })
    wx.request({
      url: app.apiUrl('c=zhuangxiu/index&a=getlist'),
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        area: areaID,
        decoration: decorationID,
        style: styleID,
        //page: page,
        city_id: id
      },
      success: function(res) {
        var polist = res.data.data
        var arr1 = []
        var arr2 = []
        var stars = []
        var halfs = []
        for (var i in polist) {
          var latitude = polist[i].latitude;
          var longitude = polist[i].longitude
          var star = parseInt(polist[i].compose)
          var half = 5 - star
          if (half > 0.5) {
            half = 1
            star = 5 - half
          }
          if ((polist[i].latitude == null) || (polist[i].longitude == null)) {
            polist[i].state = false
          } else {
            polist[i].state = true
          }
          stars.push(star)
          halfs.push(half)
          arr1.push(latitude)
          arr2.push(longitude)
        }
        that.setData({
          postlists: polist,
          getlatitude: arr1,
          getlongitude: arr2,
          star: stars,
          half: halfs
        })
        setTimeout(function() {
          that.star()
        }, 500)
      },
      fail: function() {
        // fail
      },
      complete: function() {
        // complete
        setTimeout(function() {
          wx.hideLoading()
        }, 2000)
        wx.stopPullDownRefresh() //停止下拉刷新
      }
    })
  },
  skipTo: function(e) {
    var theId = e.currentTarget.dataset.theid;
    var id = e.currentTarget.dataset.comid
    var dist = e.currentTarget.dataset.distance
    // console.log(dist)
    var num = parseInt(e.currentTarget.dataset.index);
    wx.navigateTo({
      url: "decorcompany_detail?id=" +id
    })
    //console.log(id);
  },
  //点击其他地方隐藏
  hiddenothers: function() {
    var sames_image = "https://img.xtuan.com/xcximgtest/images/materal/down.png"
    var other = "rgb(153,153,153)"
    this.setData({
      showacerage: false,
      showhouse: false,
      showprice: false,
      showMask: false,
      icon_imagef: sames_image,
      icon_imagec: sames_image,
      icon_imaget: sames_image,
      the_color: other,
      the_hcolor: other,
      the_pcolor: other,
    })
  }
})